<template>
  <div id="app">
    <mt-popup class="app-popup" :modal="false" v-model="popupVisible" position="top">{{wsConten}}</mt-popup>
    <transition :name="transitionName">
      <!-- <keep-alive> -->
      <router-view></router-view>
      <!-- </keep-alive> -->
    </transition>
  </div>
</template>

<style>
.mint-spinner-snake {
  display: inline-block;
  border-top-color: rgb(71, 135, 228) !important;
  border-left-color: rgb(71, 135, 228) !important;
  border-bottom-color: rgb(71, 135, 228) !important;
}
.app-popup {
  background-color: rgba(0, 0, 0, 0.5);
  color: #fff;
  font-size: 16px;
  height: 50px;
  width: 100vw;
  line-height: 50px;
  text-align: center;
}
.iconbulb {
  font-size: 12px;
  color: #eabd6f;
}
.mint-navbar {
  border-bottom: 0.5px solid #eeeeee;
}
.mint-navbar .mint-tab-item.is-selected {
  margin-bottom: -1px;
}
.mint-search {
  height: 44px;
}
.iconxingming {
  color: #4787e3;
  font-weight: 700;
}
.assess-item .mint-cell-wrapper {
  background: none;
}
.mint-radiolist-title {
  display: none;
}
.mint-radio-label {
  margin-left: 0;
}
.assess-item .mint-cell {
  height: 20px !important;
  min-height: 0;
  margin-right: 10px;
}
.assess-item .mint-cell-wrapper,
.assess-item .mint-radiolist-label {
  padding: 0;
}
.mint-radio-core {
  width: 10px;
  height: 10px;
}
.mint-radio-core::after {
  top: 0;
  left: 0;
  width: 0px;
  height: 0px;
}
.mint-radio-label {
  font-size: 10px;
}
.clearfix:after {
  content: " ";
  display: block;
  height: 0;
  clear: both;
}
.bscroll {
  width: 100%;
  height: 100vh;
  overflow: hidden;
}
.mint-toast {
  z-index: 99999 !important;
}
.mint-header {
  background-color: #4787e3;
}
.mint-search-list {
  display: none !important;
}
.mint-searchbar {
  background-color: #ffffff !important;
}
.mint-searchbar-core,
.mint-searchbar-inner {
  background-color: #eeeeee !important;
  text-align: left;
}
#app {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  height: 100vh;
  overflow: hidden;
  /* border: 1px solid transparent; */
  box-sizing: border-box;
  touch-action: none;
}
.slide-right-enter-active,
.slide-right-leave-active,
.slide-left-enter-active,
.slide-left-leave-active {
  transition: all 500ms;
  position: absolute;
}
.slide-right-enter {
  opacity: 0;
  transform: translate3d(-100%, 0, 0);
}
.slide-right-leave-active {
  opacity: 0;
  transform: translate3d(100%, 0, 0);
}
.slide-left-enter {
  opacity: 0;
  transform: translate3d(100%, 0, 0);
}
.slide-left-leave-active {
  opacity: 0;
  transform: translate3d(-100%, 0, 0);
}
</style>

<script>
import { MessageBox } from "mint-ui";
export default {
  name: "App",
  data() {
    return {
      transitionName: "",
      loadingImg: false,
      popupVisible: false,
      wsConten: ""
    };
  },
  mounted() {
    let that = this;
    let websocket = "";
    wsConcat();
    function wsConcat() {
      //消息推送 home
      that
        .$get("websocket")
        .then(res => {
          if (res.success) {
            if ("WebSocket" in window) {
              websocket = new WebSocket(res.data.WebSocketUrl);
              //连接发生错误的回调方法
              websocket.onerror = function() {
                //发生连接错误的处理代码
                MessageBox.alert("消息推送错误", "提示");
              };
              //连接成功建立的回调方法
              websocket.onopen = function(event) {
                console.warn("websocket连接成功");
              };
              //接收到消息的回调方法
              websocket.onmessage = function(event) {
                if (event.data != null && event.data != "") {
                  let data = JSON.parse(event.data);
                  that.wsConten = data.content;
                  if (data.content) {
                    that.popupVisible = true;
                    setTimeout(() => {
                      that.popupVisible = false;
                    }, 4000);
                  }
                  console.log(data);
                }
              };
              //连接关闭的回调方法
              websocket.onclose = function() {
                //连接关闭时的处理代码
                wsConcat();
              };
            } else {
              alert("不支持吗");
            }
          }
        })
        .catch(res => {
          return false;
        });

      //关闭WebSocket连接
      function closeWebSocket() {
        websocket.close();
      }
    }
  },
  methods: {},
  watch: {
    // 使用watch 监听$router的变化
    $route(to, from) {
      let controls = false;
      let that = this;
      console.log(to.path);
      if(to.path == '/applyClass'){
        controls = true;
      }
      dd.biz.navigation.setLeft({
        control: controls, //是否控制点击事件，true 控制，false 不控制， 默认false
        onSuccess: function(result) {
          //如果control为true，则onSuccess将在发生按钮点击事件被回调
          that.$router.push({
            path:'/studyCenter'
          })
        },
        onFail: function(err) {}
      });
      // 如果to索引大于from索引,判断为前进状态,反之则为后退状态
      if (to.meta.index > from.meta.index) {
        // 设置动画名称
        this.transitionName = "slide-left";
      } else {
        this.transitionName = "slide-right";
      }
    }
  }
};
</script>
